﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Internal;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YZQ_DotNetArx.CAD;

namespace YZQ_DotNetArx.Test
{
    public class 扩展数据
    {
        [CommandMethod("CK")]
        public static void CK()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            doc.SendStringToExecute("(entget (car (entsel )) '(\"*\")) ", true, false, false);
        }
        [CommandMethod("AddX")]
        public static void AddX()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            //提示用户选择一个表示董事长的多行文本
            PromptEntityOptions peo = new PromptEntityOptions("\n请选则表示董事长的多行文本");
            peo.SetRejectMessage("\n您选则的不是直线，请重新选则");
            peo.AddAllowedClass(typeof(Line), true);
            PromptEntityResult per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK) return;
            ObjectId id = per.ObjectId; //用户选则的多行文本的ObjectId
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                TypedValueList values = new TypedValueList();
                //添加整型（表示员工编号）和字符串（表示职位）扩展数据项
                values.Add(DxfCode.ExtendedDataInteger32, 1002);
                values.Add(DxfCode.ExtendedDataAsciiString, "董事长");
                //为实体添加应用程序名为"EMPLOYEE"的扩展数据
                id.AddXData("EMPLOYEE", values);
                trans.Commit();
            }
        }
        //开始鼠标监控事件
        [CommandMethod("StartMonitor")]
        public static void StartMonitor()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //开始鼠标监控事件
            ed.PointMonitor += new PointMonitorEventHandler(Ed_PointMonitor);         
        }
        //鼠标监控事件的处理函数
        static void Ed_PointMonitor(object sender, PointMonitorEventArgs e)
        {
            string employeeInfo = "";   //用于存储员工的信息：编号和职位
            //获取命令行对象（鼠标监控事件的发起者）,用于获取文档对象
            Editor ed = (Editor)sender;
            Document doc = ed.Document;
            //获取鼠标停留处的实体
            FullSubentityPath[] paths = e.Context.GetPickedEntities();
            using (Transaction trans = doc.TransactionManager.StartTransaction())
            {
                //如果鼠标停留处有实体
                if (paths.Length > 0)
                {
                    //获取鼠标停留处的实体
                    FullSubentityPath path = paths[0];
                    Line line = trans.GetObject(path.GetObjectIds()[0], OpenMode.ForRead) as Line;
                    if (line != null) //如果停留处为多行文本
                    {
                        //获取多行文本中应用程序为"EMPLOYEE"的扩展数据
                        TypedValueList xdata = line.ObjectId.GetXData("EMPLOYEE");
                        if (xdata != null)
                        {
                            employeeInfo += "员工编号:" + xdata[1].Value.ToString() + "\n职位:" + xdata[2].Value.ToString();
                            Env.Print(employeeInfo);
                        }
                    }
                }
                trans.Commit();
            }
           

            if (employeeInfo != "")
            {
                try
                {
                    string str = "测试" + "\n测试" + "\n测试" + "\n测试" + "\n测试";
                    e.AppendToolTipText(str); 
                    Env.Print(employeeInfo);
                }
                catch (System.Exception)
                {

                    throw;
                }
             
            }
        }
 
        //停止鼠标监控事件
        [CommandMethod("StopMoniter")]
        public static void StopMoniter()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //停止鼠标监控事件
            ed.PointMonitor -= new PointMonitorEventHandler(Ed_PointMonitor);
        }


    }
}
